import { createRouter, createWebHashHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const router = createRouter({
  // history: createWebHistory(import.meta.env.BASE_URL),
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView
    },
    {
      path: '/about',
      name: 'about',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/AboutView.vue')
    },
    {
      path: '/pinia/base',
      name: 'base',
      component: () => import('../views/PiniaBaseView.vue')
    },
    {
      path: '/pinia/better',
      name: 'better',
      component: () => import('../views/PiniaBetterView.vue')
    },
    {
      path: '/pinia/setup',
      name: 'setup',
      component: () => import('../views/PiniaSetupView.vue')
    },
    {
      path: '/404',
      name: 'notfound',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/NofoundView.vue')
    },
    {
      // path:"*",    vue-router@3 的写法
      path: "/:pathMatch(.*)*",   // vue-router@4 的写法
      redirect: "/404",
      // redirect:(from)=>{
      //   console.log(from);
      //   return {path:"/404",query:{wd:from.params.pathMatch}};
      // }
    }
  ]
})



// vue-router @3 / 4
// router.beforeEach((to,from,next)=>{
//     console.log("from",from);
//     console.log("to",to);
//     next()
// })

/* router.beforeEach((to, from) => {
  console.log("from", from);
  console.log("to", to);
  // return true;  // next();

  // if (to.path == "/about") {
  //   return false;  //  next(false);
  // }
  // return true;

  if (to.path == "/about" ) {
      // return "/404";  // next("/404")
      return {path:"/404",query:{a:1,b:2}};  // next("/404")
  }

  return true; // next()

})
 */



export default router
